<template>
	<view class="content">
<button open-type='getUserInfo'  v-if="!is_show">获取授权</button>

	<view class="input-group" v-if="is_show">
			
		
			<view class="input-row">

				<m-input class="m-input" type="text" clearable focus v-model="account" placeholder="请输入账号"></m-input>
			</view>
			<view class="input-row">

				<m-input type="password" displayable v-model="pwd" placeholder="请输入密码"></m-input>
				
			</view>
			<view class="btn-row">
				<button type="primary" class="primary" @tap="bindLogin">登录</button>
			</view>
	</view>

<!-- 	<view class="loading">
	     <text>加载中,等一小下</text>
	     <view class="rect"></view>
	</view> -->

	</view>
</template>

<script>
	import data from '../../datas/data.json'
	import store from '../../globalStore/store.json'
	import mInput from '../component/m-input.vue'

	export default {
		components: {
			mInput
		},
		data() {
			return {
				account: '',
				pwd: '',
				is_show:true,
			}
		},
		onLoad() {

			//#ifdef MP-WEIXIN
			this.is_show=false
				this.wxLogin()
			//#endif	
		},
		onShow() {
			uni.showLoading();

			setTimeout(function () {
				uni.hideLoading();
			}, 2000);
		},
		methods: {
			bindLogin() {
				var that=this;
				/**
				 * 客户端对账号信息进行一些必要的校验。
				 * 实际开发中，根据业务需要进行处理，这里仅做示例。
				 */
				// if (this.account.length < 5) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '账号最短为 5 个字符'
				// 	});
				// 	return;
				// }
				// if (this.pwd.length < 6) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '密码最短为 6 个字符'
				// 	});
				// 	return;
				// }
				/**
				 * 下面简单模拟下服务端的处理
				 * 检测用户账号密码是否在已注册的用户列表中
				 * 实际开发中，使用 uni.request 将账号信息发送至服务端，客户端在回调函数中获取结果信息。
				 */
				const msg = {
					user_name: this.account,
					password: this.pwd,
					"sex":"女",
					"icon":"https://ossweb-img.qq.com/images/lol/web201310/skin/big10001.jpg",
				};
				//console.log(msg)
							uniCloud.callFunction({
								name:"register",
								data:msg,
								success(res) {
									store.user=res.result.user;
									store.user.user_id=res.result.user._id;


									uni.reLaunch({
										url: '../home/home',
										success(res) {
											that.$goEasy.subscribe({
												channel: store.user.user_id, //替换为您自己的channel
												onMessage: function (message) {
													let chat_datas=store.chat_datas;
													let lists;
													store.chats_count++;

													console.log(message);
													let user_id=message.content.split("@")[0];
													let user_name=message.content.split("@")[1];
													let time=message.content.split("@")[3];
													let content=message.content.split("@")[2];
													let icon=message.content.split("@")[4];
													let msg={
														
														"icon":icon,
														"content":content,
														"time":time,
														"user_id":user_id,
														"user_name":user_name
													};
													if(chat_datas.length==0){
														let tmp_chat={
															"friend_id":user_id,
															"friend_name":user_name,
															"icon":icon,
															msgs:[]
														}
														chat_datas.push(tmp_chat)
														lists=chat_datas[0].msgs
													}else{
														let status=0;
														let index=0;
														for(let i=0; i<chat_datas.length; i++){
															if(chat_datas[i].friend_id==user_id){
																status=1;
																index=i;
																break;
															}
														}
														if(status==1){
															lists=chat_datas[index].msgs
														}else{
															let tmp_chat={
																"friend_id":user_id,
																"friend_name":user_name,
																"icon":icon,
																msgs:[]
															}
															chat_datas.push(tmp_chat)
															lists=chat_datas[chat_datas.length-1].msgs				
														}
													}													
													lists.push(msg);
													console.log(store);
													//console.log("Channel:" + message.channel + " content:" + message.content);
												}
											});																		
										}
									});

								}
							})
				// let lists=data.users;
				// let status=0;
				// for(let i=0; i<data.users.length; i++){
				// 	if(this.account==data.users[i].user_name){
				// 		if(this.pwd==data.users[i].password){
				// 			status=1;
				// 			store.user=data.users[i];
				// 			this.$goEasy.subscribe({
				// 				channel: data.users[i].user_id, //替换为您自己的channel
				// 				onMessage: function (message) {
				// 					// let msg={
										
				// 					// 	icon:"",
				// 					// 	content:message.content.split(":")[1],
				// 					// 	time:getDate(),
				// 					// 	user_id:message.content.split(":")[0]
				// 					// };

				// 					// that.lists.push(msg);
				// 					console.log("Channel:" + message.channel + " content:" + message.content);
				// 				}
				// 			});							
				// 			// store.user.user_name=lists[i].user_name;
				// 			// store.user.user_id=lists[i].user_id;
				// 			// store.user.icon=lists[i].icon;
				// 			// store.user.frends=lists[i].focused_users[0]

				// 			uni.reLaunch({
				// 				url: '../disconver/disconver',
				// 			});
				// 			break;							
				// 		}else{
				// 			uni.showToast({
				// 				icon: 'none',
				// 				title: '密码错误'
				// 			});							
				// 		}

					// }
				// }
				// if(status==0){
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '该账号不存在',
				// 		duration: 1500
				// 	});
					
				// }
			},
			wxLogin(){
				var that=this;
				// wx登录
				wx.login({
				  success (res) {
					if (res.code) {
					  //发起网络请求
					  var code = res.code
						// 获取微信用户信息
						wx.getUserInfo({
						  success: function(res) {
							var userInfo = res.userInfo
							var nickName = userInfo.nickName
							var icon = userInfo.avatarUrl
							var gender = userInfo.gender==1?"男":"女";//性别 0：未知、1：男、2：女
							let user={	
								"user_name":nickName,
								"password":"wxpwd",
								"sex":gender,
								"icon":icon,
							};
							//data.users.push(user);
							uniCloud.callFunction({
								name:"register",
								data:user,
								success(res) {
									store.user=res.result.user;
									store.user.user_id=res.result.user._id;

									uni.reLaunch({
										url: '../home/home',
										success(res) {
											that.$goEasy.subscribe({
												channel: store.user.user_id, //替换为您自己的channel
												onMessage: function (message) {
													let chat_datas=store.chat_datas;
													let lists;
													store.chats_count++;

													console.log(message);
													let user_id=message.content.split("@")[0];
													let user_name=message.content.split("@")[1];
													let time=message.content.split("@")[3];
													let content=message.content.split("@")[2];
													let icon=message.content.split("@")[4];
													let msg={
														
														"icon":icon,
														"content":content,
														"time":time,
														"user_id":user_id,
														"user_name":user_name
													};
													if(chat_datas.length==0){
														let tmp_chat={
															"friend_id":user_id,
															"friend_name":user_name,
															"icon":icon,
															msgs:[]
														}
														chat_datas.push(tmp_chat)
														lists=chat_datas[0].msgs
													}else{
														let status=0;
														let index=0;
														for(let i=0; i<chat_datas.length; i++){
															if(chat_datas[i].friend_id==user_id){
																status=1;
																index=i;
																break;
															}
														}
														if(status==1){
															lists=chat_datas[index].msgs
														}else{
															let tmp_chat={
																"friend_id":user_id,
																"friend_name":user_name,
																"icon":icon,
																msgs:[]
															}
															chat_datas.push(tmp_chat)
															lists=chat_datas[chat_datas.length-1].msgs				
														}
													}													
													lists.push(msg);
												}
											});							
										}
									});
								}
							})
							

						  },
						  fail:res=>{
							  // 获取失败的去引导用户授权 
							  console.log(res)
						   }
						})
						
					}
				  }
				})
			},
			toMain(user) {
				this.globalLogin(user);

				uni.reLaunch({
					url: '../disconver/disconver',
				});

			},
		}

	}
</script>

<style scoped>
	@import "../component/m-icon/m-icon.css";
	/*每个页面公共css */
	page {
		min-height: 100%;
		display: flex;
		font-size: 16px;
	}

	/* #ifdef MP-BAIDU */
	page {
		width: 100%;
		height: 100%;
		display: block;
	}

	swan-template {
		width: 100%;
		min-height: 100%;
		display: flex;
	}

	/* 原生组件模式下需要注意组件外部样式 */
	custom-component {
		width: 100%;
		min-height: 100%;
		display: flex;
	}

	/* #endif */

	/* #ifdef MP-ALIPAY */
	page {
		min-height: 100vh;
	}

	/* #endif */

	/* 原生组件模式下需要注意组件外部样式 */
	m-input {
		width: 100%;
		/* min-height: 100%; */
		display: flex;
		flex: 1;
	}

	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #f9f9f9;
		padding: 10px;
	}

	.input-group {

		margin-top: 20px;
		position: relative;
	}

	/* .input-group::before {
		position: absolute;
		right: 0;
		top: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}

	.input-group::after {
		position: absolute;
		right: 0;
		bottom: 0;
		left: 0;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	} */

	.input-row {
		box-shadow: #ebedf2 0px 15px 33px;
		display: flex;
		background: #FFFFFF;
		height: 50px;
		flex-direction: row;
		position: relative;
		font-size: 18px;
		line-height: 40px;
		border-radius: 20px 20px;
	}
	.input-row:not(:first-child){
		margin-top:6%;
	}
	.input-row .title {
		width: 72px;
		padding-left: 15px;
	}

	.input-row.border::after {
		position: absolute;
		right: 0;
		bottom: 0;
		left: 8px;
		height: 1px;
		content: '';
		-webkit-transform: scaleY(.5);
		transform: scaleY(.5);
		background-color: #c8c7cc;
	}

	.btn-row {
		margin-top: 25px;
		padding: 10px;
	}

	button.primary {
		background: linear-gradient(#2b7ffc, #4c9cfd);
		font-size: 20px;
		border-radius: 20px;
	}

	.logo{
		margin-left: 38%;
		margin-top:11%;
		margin-bottom: 3%;
		width: 90px;
		height:70px;
	}
	.action-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		
		margin-top: 7%;
	}
	.forget{
		margin-top: 7%;
	}
	.forget-nav{
		margin-left: 70%;
	}
	.top-nav{
		width: 107%;
		height: 40px;
		margin-left: -3%;
		margin-top: -3%;
		display: flex;
		flex-direction: row;
		justify-content: center;
		background: #FFFFFF;
	}
	.top-nav navigator {
		color: #2478fc;
		font-size: 20px;
		padding: 0 10px;
	}
	.action-row navigator {
		color: #007aff;
		padding: 0 10px;
	}

	.oauth-row {
		display: flex;
		flex-direction: row;
		justify-content: center;
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
	}

	.oauth-image {
		position: relative;
		width: 50px;
		height: 50px;
		border: 1px solid #dddddd;
		border-radius: 50px;
		margin: 0 20px;
		background-color: #ffffff;
	}

	.oauth-image image {
		width: 30px;
		height: 30px;
		margin: 10px;
	}

	.oauth-image button {
		position: absolute;
		left: 0;
		top: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
</style>
